// 链表数据结构

class Node {
    constructor (element) {
        this.element = element;
        this.next = null;
    }
}

class LinkedList {
    constructor () {
        this.start = null;
        this.length = 0;
    }
    // 向末尾添加节点
    append (element) {
        let curNode = this.start;
        let index = 0;
        let newNode = new Node(element);

        if (!curNode) {
            this.start = newNode;
        }
        else {
            while (curNode && curNode.next && curNode.next !== this.start) {
                curNode = curNode.next;
            }
            curNode.next = newNode;
        }
        return true;
    }
    // 获得所有数据(Array)
    all () {
        let res = [];
        let curNode = this.start;
        while (curNode) {
            res.push(curNode);
            curNode = curNode.next;

            if (curNode === this.start) break;
        }
        return res;
    }
    // 指定位置插入节点
    insert (position, element) {
        if (!(position >= 0 && position <= this.length)) {
            return false;
        }

        let curNode = this.start;
        let newNode = new Node(element);
        let index = 0;
        let previous;

        if (position === 0) {
            newNode.next = curNode;
            this.start = newNode;
        }
        else {
            while (index++ !== position) {
                previous = curNode;
                curNode = curNode.next;
            }

            previous.next = newNode;
            newNode.next = curNode;
        }
        this.length++;

        return true;
    }
    // 移除指定位置节点
    removeAt (position) {
        if (!(position >= 0 && position < this.length)) {
            return false;
        }

        let curNode = this.start;
        let index = 0;
        let previous;

        if (position === 0) {
            this.start = curNode.next;
        }
        else {
            while (index++ !== position) {
                previous = curNode;
                curNode = curNode.next;
            }
            previous.next = null;
        }

        this.length--;

        return true;
    }
    // 根据索引值，查找指定节点
    find (position) {
        if (!(position >= 0 && position < this.length)) {
            return null;
        }

        let curNode = this.start;
        let index = 0;

        while (index++ !== position) {
            curNode = curNode.next;
        }
        return curNode;
    }
    // 移除指定数据节点
    removeEl (element) {
        let data = this.all();
        let previous = this.start;
        let curNode;
        
        for(let i=0,len=data.length; i<len; i++) {
            let item = data[i];
            if (item.element === element) {
                curNode = item;
                break;
            }
            previous = item;
        };

        if (curNode) {
            previous.next = curNode.next;
            return true;
        }
        return false;
    }
    // 删除最后节点
    pop () {
        return this.removeAt(this.length - 1);
    }
    // 返回索引值
    indexOf (element) {
        let res = -1;
        let index = 0;
        let curNode = this.start;

        while (curNode) {
            if (curNode.element === element) {
                res = index;
                break;
            }
            curNode = curNode.next;
            index++;
        }
        return res;
    }
    // 是否为空
    isEmpty () {
        return this.length === 0;
    }
    // 尺寸
    size () {
        return this.length;
    }
    // 输出字符串
    toString () {
        let curNode = this.start;
        let res = '';

        while (curNode) {
            res += curNode.element;
            curNode = curNode.next;
        }
        return res;
    }
    // 获取第一个节点
    header () {
        return this.start;
    }
    // 获取最后一个节点
    end () {
        let curNode = this.start;
        while (curNode && curNode.next) {
            curNode = curNode.next;
        }
        return curNode;
    }
}